package com.stonedt.spider.controller.admin;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.stonedt.spider.entity.BidEntity;
import com.stonedt.spider.entity.NewEntity;
import com.stonedt.spider.entity.ServerResource;
import com.stonedt.spider.service.IServerResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.*;



@Controller
@RequestMapping("/admin")
public class AdminController {
	@Autowired
	private IServerResourceService serverResourceService;
	
	//数据源
	@RequestMapping(value="/datasource",produces = "text/plain;charset=utf-8")
	public ModelAndView datasource(ModelAndView mv,HttpServletResponse response,HttpServletRequest request){
		List<ServerResource> list = serverResourceService.queryServerResourceAll();
		mv.addObject("list",list);
		mv.setViewName("datasource");
		return mv;
	}
	
	//站点管理
	@RequestMapping(value="/website_admin",produces = "text/plain;charset=utf-8")
	public ModelAndView website_admin(ModelAndView mv, @RequestParam(value = "pageNum", defaultValue = "1", required = false) Integer pageNum){
//		PageHelper.startPage(pageNum, 30);
//		List<Map<String, Object>> listHotWords = serverResourceService.listGov(3);
//		for (Map<String, Object> map : listHotWords) {
//			Object object = map.get("create_date");
//			map.put("create_date", object.toString().substring(0, 10));
//		}
//		PageInfo<Map<String, Object>> pageInfo = new PageInfo<Map<String,Object>>(listHotWords);
//		mv.addObject("list", listHotWords);
//		mv.addObject("pageInfo", pageInfo);
		mv.setViewName("datasource/website_admin");
		return mv;
	}
	
	//招投标管理
	@RequestMapping(value="/website_bid",produces = "text/plain;charset=utf-8")
	public ModelAndView website_bid(ModelAndView mv, @RequestParam(value = "pageNum", defaultValue = "1", required = false) Integer pageNum){
		mv.setViewName("datasource/website_bid");
		return mv;
	}
	
	@RequestMapping(method = RequestMethod.POST, value = "/list", produces = "application/json;charset=utf-8")
	@ResponseBody
	public JSONObject list(NewEntity params){
		JSONObject result=new JSONObject();
		if(params.getTypes()!=null) {
			String[] split = params.getTypes().split(",");
			List<Integer> typeList=new ArrayList<Integer>();
			for (String string : split) {
				typeList.add(Integer.valueOf(string));
			}
			params.setTypeList(typeList);
		}
		PageHelper.startPage(params.getPageNum(), 30);
		List<NewEntity> listHotWords = serverResourceService.list(params);
		for (NewEntity map : listHotWords) {
			Date create_date = map.getCreate_date();
			map.setDate(new SimpleDateFormat("yyyy-MM-dd").format(create_date));
		}
		PageInfo<NewEntity> pageInfo = new PageInfo<NewEntity>(listHotWords);
		result.put("list", listHotWords);
		result.put("pageInfo", pageInfo);
		return result;
	}
	
	@RequestMapping(method = RequestMethod.POST, value = "/Bidlist", produces = "application/json;charset=utf-8")
	@ResponseBody
	public JSONObject Bidlist(NewEntity params){
		JSONObject result=new JSONObject();
		if(params.getTypes()!=null) {
			String[] split = params.getTypes().split(",");
			List<Integer> typeList=new ArrayList<Integer>();
			for (String string : split) {
				typeList.add(Integer.valueOf(string));
			}
			params.setTypeList(typeList);
		}
		PageHelper.startPage(params.getPageNum(), 30);
		List<NewEntity> listHotWords = serverResourceService.Bidlist(params);
		for (NewEntity map : listHotWords) {
			Date create_date = map.getCreate_date();
			map.setDate(new SimpleDateFormat("yyyy-MM-dd").format(create_date));
		}
		PageInfo<NewEntity> pageInfo = new PageInfo<NewEntity>(listHotWords);
		result.put("list", listHotWords);
		result.put("pageInfo", pageInfo);
		return result;
	}
	
	@RequestMapping(value = "/loadSponsorNature", method = RequestMethod.GET, produces = "application/json;charset=utf-8")
	@ResponseBody
	public Set<String> loadSponsorNature(){
		Set<String> listSponsorNature=serverResourceService.loadSponsorNature();
		listSponsorNature.remove("无");
		listSponsorNature.remove(null);
		return listSponsorNature;
	}
	
	@RequestMapping(value = "/loadSponsorNatureBid", method = RequestMethod.GET, produces = "application/json;charset=utf-8")
	@ResponseBody
	public Set<String> loadSponsorNatureBid(){
		Set<String> listSponsorNature=serverResourceService.loadSponsorNatureBid();
		listSponsorNature.remove("无");
		listSponsorNature.remove(null);
		return listSponsorNature;
	}
	
	@RequestMapping(value = "/updateWebType", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
	@ResponseBody
	public String updateWebType(Integer id, Integer webType) {
		serverResourceService.updateWebType(id, webType);
		return "OK";
	}
	
	@RequestMapping(value="/gov",produces = "text/plain;charset=utf-8")
	public ModelAndView gov(ModelAndView mv,@RequestParam(value = "pageNum", defaultValue = "1", required = false) Integer pageNum) {
		PageHelper.startPage(pageNum, 30);
		List<Map<String, Object>> listHotWords = serverResourceService.listGov(3);
		for (Map<String, Object> map : listHotWords) {
			Object object = map.get("create_date");
			map.put("create_date", object.toString().substring(0, 10));
		}
		PageInfo<Map<String, Object>> pageInfo = new PageInfo<Map<String,Object>>(listHotWords);
		mv.addObject("list", listHotWords);
		mv.addObject("pageInfo", pageInfo);
		mv.setViewName("datasource/gov");
		return mv;
	}
	
	@RequestMapping(value="/news",produces = "text/plain;charset=utf-8")
	public ModelAndView news(ModelAndView mv,@RequestParam(value = "pageNum", defaultValue = "1", required = false) Integer pageNum) {
		PageHelper.startPage(pageNum, 30);
		List<Map<String, Object>> listHotWords = serverResourceService.listNews(5);
		for (Map<String, Object> map : listHotWords) {
			Object object = map.get("create_date");
			map.put("create_date", object.toString().substring(0, 10));
		}
		PageInfo<Map<String, Object>> pageInfo = new PageInfo<Map<String,Object>>(listHotWords);
		mv.addObject("list", listHotWords);
		mv.addObject("pageInfo", pageInfo);
		mv.setViewName("datasource/news");
		return mv;
	}
	
	@RequestMapping(value="/forum",produces = "text/plain;charset=utf-8")
	public ModelAndView forum(ModelAndView mv,@RequestParam(value = "pageNum", defaultValue = "1", required = false) Integer pageNum) {
		PageHelper.startPage(pageNum, 30);
		List<Map<String, Object>> listHotWords = serverResourceService.listForum(4);
		for (Map<String, Object> map : listHotWords) {
			Object object = map.get("create_date");
			map.put("create_date", object.toString().substring(0, 10));
		}
		PageInfo<Map<String, Object>> pageInfo = new PageInfo<Map<String,Object>>(listHotWords);
		mv.addObject("list", listHotWords);
		mv.addObject("pageInfo", pageInfo);
		mv.setViewName("datasource/forum");
		return mv;
	}
	
	@RequestMapping(value="/newspaper",produces = "text/plain;charset=utf-8")
	public ModelAndView newspaper(ModelAndView mv,@RequestParam(value = "pageNum", defaultValue = "1", required = false) Integer pageNum) {
		PageHelper.startPage(pageNum, 30);
		List<Map<String, Object>> listHotWords = serverResourceService.listNewspaper(6);
		for (Map<String, Object> map : listHotWords) {
			Object object = map.get("create_date");
			map.put("create_date", object.toString().substring(0, 10));
		}
		PageInfo<Map<String, Object>> pageInfo = new PageInfo<Map<String,Object>>(listHotWords);
		mv.addObject("list", listHotWords);
		mv.addObject("pageInfo", pageInfo);
		mv.setViewName("datasource/newspaper");
		return mv;
	}
	
	@RequestMapping(value="/blog",produces = "text/plain;charset=utf-8")
	public ModelAndView blog(ModelAndView mv,@RequestParam(value = "pageNum", defaultValue = "1", required = false) Integer pageNum) {
		PageHelper.startPage(pageNum, 30);
		List<Map<String, Object>> listHotWords = serverResourceService.listBlog(11);
		for (Map<String, Object> map : listHotWords) {
			Object object = map.get("create_date");
			map.put("create_date", object.toString().substring(0, 10));
		}
		PageInfo<Map<String, Object>> pageInfo = new PageInfo<Map<String,Object>>(listHotWords);
		mv.addObject("list", listHotWords);
		mv.addObject("pageInfo", pageInfo);
		mv.setViewName("datasource/blog");
		return mv;
	}
	
	@RequestMapping(value="/website",produces = "text/plain;charset=utf-8")
	public ModelAndView website(ModelAndView mv,@RequestParam(value = "pageNum", defaultValue = "1", required = false) Integer pageNum) {
		PageHelper.startPage(pageNum, 30);
		List<Map<String, Object>> listHotWords = serverResourceService.listWebsite(8);
		for (Map<String, Object> map : listHotWords) {
			Object object = map.get("create_date");
			map.put("create_date", object.toString().substring(0, 10));
		}
		PageInfo<Map<String, Object>> pageInfo = new PageInfo<Map<String,Object>>(listHotWords);
		mv.addObject("list", listHotWords);
		mv.addObject("pageInfo", pageInfo);
		mv.setViewName("datasource/website");
		return mv;
	}
	
	//服务器列表
	@RequestMapping(value="/toserverlist",produces = "text/plain;charset=utf-8")
	public ModelAndView toserverlist(ModelAndView mv,HttpServletResponse response,HttpServletRequest request){
		mv.setViewName("server_list");
		return mv;
	}
	
	//系统概览
	@RequestMapping(value="/tosystem_overview",produces = "text/plain;charset=utf-8")
	public ModelAndView tosystem_overview(ModelAndView mv,HttpServletResponse response,HttpServletRequest request){
		List<ServerResource> list = serverResourceService.queryServerResourceAll();
		mv.addObject("list",list);
		mv.setViewName("dashboard");
		return mv;
	}
	
	//系统概览
		@RequestMapping(value="/tosystem_overview_factory",produces = "text/plain;charset=utf-8")
		public ModelAndView tosystem_overview_factory(ModelAndView mv,HttpServletResponse response,HttpServletRequest request){
			List<ServerResource> list = serverResourceService.queryServerResourceAll();
			mv.addObject("list",list);
			mv.setViewName("system_overview-factory");
			return mv;
		}
	
	@RequestMapping(value="/fail",produces = "text/plain;charset=utf-8")
	public ModelAndView fail(ModelAndView mv,HttpServletResponse response,HttpServletRequest request){
		
		mv.setViewName("fail");
		return mv;
	}
	@RequestMapping(value="/success",produces = "text/plain;charset=utf-8")
	public ModelAndView success(ModelAndView mv,HttpServletResponse response,HttpServletRequest request){
		
		mv.setViewName("success");
		return mv;
	}
	
	@RequestMapping(value="/left",produces = "text/plain;charset=utf-8")
	public ModelAndView left(ModelAndView mv,HttpServletResponse response,HttpServletRequest request){
		
		mv.setViewName("left");
		return mv;
	}
	
	@RequestMapping(value="/top",produces = "text/plain;charset=utf-8")
	public ModelAndView top(ModelAndView mv,HttpServletResponse response,HttpServletRequest request){
		
		mv.setViewName("components/top");
		return mv;
	}
	
	
	@RequestMapping(value="/footer",produces = "text/plain;charset=utf-8")
	public ModelAndView footer(ModelAndView mv,HttpServletResponse response,HttpServletRequest request){
		
		mv.setViewName("components/footer");
		return mv;
	}



	@RequestMapping(value="/disabled_column",produces = "text/plain;charset=utf-8")
	public ModelAndView disabled_column(ModelAndView mv){
		mv.setViewName("datasource/disabled_column");
		return mv;
	}
}
